﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.SuperGrid;
using BUS;
using DTO;

namespace BCSALE
{
    public enum BanHang
    {
        BAN_LE  = 1,
        BAN_SI = 2
    }
    public partial class frmBanHang : DevComponents.DotNetBar.Office2007Form
    {
        private BanHang _loaiBanHang;
        private BindingSource _bdsKhachHang;
        private BindingSource _bdsHangHoa;
        private int _stt;

        public int Stt
        {
            get { return _stt; }
            set { _stt = value; }
        }

        public BanHang LoaiBanHang
        {
            get { return _loaiBanHang; }
            set { _loaiBanHang = value; }
        }

        public void initDataSource()
        {
            //Khach hang Datasource
            this._bdsKhachHang = new BindingSource();            
            this._bdsKhachHang.DataSource = KhachHangBUS.loadAll();

            cbxMaKhachHang.DataSource = this._bdsKhachHang;
            cbxMaKhachHang.DisplayMember = "MaKH";
            cbxMaKhachHang.ValueMember = "MaKH";

            cbxTenKhachHang.DataSource = this._bdsKhachHang;
            cbxTenKhachHang.DisplayMember = "TenKH";
            cbxTenKhachHang.ValueMember = "MaKH";

            //Hang hoa Datasource
            this._bdsHangHoa = new BindingSource();
            this._bdsHangHoa.DataSource = BUS.HangHoaBUS.loadAll();

            cbxMaHang.DataSource = this._bdsHangHoa;
            cbxMaHang.DisplayMember = "MaHang";
            cbxMaHang.ValueMember= "MaHang";

            cbxTenHang.DataSource = this._bdsHangHoa;
            cbxTenHang.DisplayMember = "TenHang";
            cbxTenHang.ValueMember = "MaHang";
            
            txtSoPhieu.Text = PhieuBanHangBUS.taoMaMoi();
            Stt = 0;
        }

        public frmBanHang()
        {
            InitializeComponent();
            this.LoaiBanHang = BanHang.BAN_LE;
            initDataSource();
        }

        public frmBanHang(BanHang loai)
        {
            InitializeComponent();
            string ban = "BÁN LẺ";
            if (loai == BanHang.BAN_SI)
            {
                ban = "BÁN SỈ";
            }
            lblXuatPhieuBan.Text = "XUẤT PHIẾU " + ban;
            this.LoaiBanHang = loai;
            initDataSource();
        }

        private void frmBanLe_Load(object sender, EventArgs e)
        {
            dtiNgayXuat.Value = DateTime.Now.Date;                    
        }

        private void btnThem_Click(object sender, EventArgs e)
        {            
            HangHoa h = this._bdsHangHoa.Current as HangHoa;            
            var dvt = DonViTinhBUS.loadID((int)h.MaDVT);                        
            GridRow r = new GridRow(Stt,h.MaHang,h.TenHang,dvt.TenDVT, 1,h.GiaBanSi,10,10,10);
            spgDanhSachHangHoa.ActiveGrid.Rows.Add(r);
        }
    }
}